package com.jxust.hr.controller;

import com.jxust.debate.util.CommonResult;
import com.jxust.debate.util.ResultEnum;
import com.jxust.hr.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.security.RolesAllowed;
import java.util.HashMap;
import java.util.Map;


@Api(tags="登录+其他说明类")    //请求类的说明
@RestController
@Slf4j
public class HomeController {

    @Autowired
    UserService userService;

    @ApiOperation("获取用户名")   //方法的说明
    @GetMapping(value = "/username")
    public CommonResult usernamePage(){
        //token里解析的用户名,保存到了SpringSecurity里,
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username=auth.getName().split("-")[0];//获取前面的用户名
        Map map=new HashMap<>();
        Map map2 = new HashMap();
        map2.put("userName",username);
        map2.put("role", username);  //待改进,角色不是这样获取的
        map2.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");

        map.put("userInfo",map2);

        return CommonResult.success("成功返回",
                map);

    }


    @ApiOperation("用户角色拥有权限")   //方法的说明
    @RolesAllowed({"USER", "ADMIN"})
    @GetMapping(value = "/role")
    public CommonResult rolePage(){
        return CommonResult.success(ResultEnum.SUCCESS.getMessage(),"功能待实现,固定分配: USER和ADMIN权限");

    }

    @ApiOperation("规定code的状态码")   //方法的说明
    @GetMapping(value = "/home/code")
    public CommonResult codePage(){

        Map map1;
        Map map2=new HashMap<String, Object>();
        for (ResultEnum e : ResultEnum.values()) {
            map1=new HashMap<String,String>();
            map1.put("code",e.getCode());
            map1.put("message",e.getMessage());
            map2.put(e.toString(),map1);
        }
        return CommonResult.success("成功返回",
                map2);

    }




    @ApiOperation("登录,获取token")   //方法的说明
    @ApiImplicitParams({   //方法参数的说明
            /**
             * required: 参数是否必须传
             * value: 参数的汉字说明, 解释
             * dataType: 参数类型, 默认 String, 其它值 dataType="Integer"
             * defaultValue: 参数的默认值
             **/
            @ApiImplicitParam(name="username",required=true,value="用户的姓名",dataType="String",defaultValue = "tim"),
            @ApiImplicitParam(name="password",required=true,value="用户的密码",dataType="String",defaultValue = "123")
    })
    @ApiResponses({
            @ApiResponse(code = 202, message = "用户账号或密码错误"),
            @ApiResponse(code = 203, message = "用户登录成功")
    })
    @PostMapping(value = "/login")
    public void login() {

        //TODO 这里面不需要写任何代码，由SpringSecurity的UserDeatilsService去处理

    }

/*


    @ApiOperation("返回验证码")   //方法的说明
    @GetMapping(value = "/captcha")
    public CommonResult captchaPage()throws IOException {
        String captchaCode=RandomImgCodeUtil.getStringRandom(4);
        String captchaImgBase64="data:image/png;base64,"+RandomImgCodeUtil.imageToBase64(120, 40, captchaCode);

       //TODO 这里要调用redis====功能待实现

        return new CommonResult( ResultEnum.SUCCESS.getCode(),"这里要调用redis====功能待实现",
                captchaImgBase64);

    }

*/




}
